package besta.moon.sql.expression.function.numeric;

import java.util.Random;

import besta.moon.sql.expression.function.FunctionReturnFloat;
import besta.moon.sql.parser.SQLTokenizer;

public final class FunctionRand extends FunctionReturnFloat {

    final static private Random random = new Random();

    @Override
    public final int getFunction() {
        return SQLTokenizer.RAND;
    }

    @Override
    public boolean isNull() throws Exception {
        return getParams().length == 1 && param1.isNull();
    }

    @Override
    public final double getDouble() throws Exception {
        if (getParams().length == 0) {
            return random.nextDouble();
        }
        if (isNull()) {
            return 0;
        }
        return new Random(param1.getLong()).nextDouble();
    }
}
